package com.py.purchase.service.impl;

import java.util.Collections;
import java.util.List;

import com.py.purchase.domain.*;
import com.py.purchase.mapper.PyAttachmentMapper;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.py.purchase.mapper.OdPurchaseOrderMapper;
import com.py.purchase.service.IOdPurchaseOrderService;

import javax.annotation.Resource;

/**
 * 进货订单Service业务层处理
 * 
 * @author 童佳
 * @date 2024-11-21
 */
@Service
public class OdPurchaseOrderServiceImpl implements IOdPurchaseOrderService
{
    @Resource
    private OdPurchaseOrderMapper odPurchaseOrderMapper;
    @Resource
    private PyAttachmentMapper pyAttachmentMapper;

    /**
     * 查询进货订单
     * 
     * @param phId 进货订单主键
     * @return 进货订单
     */
    @Override
    public OdPurchaseOrder selectOdPurchaseOrderByPhId(Integer phId)
    {
        OdPurchaseOrder order=odPurchaseOrderMapper.selectOdPurchaseOrderByPhId(phId);
        OdPurchaseDetails odPurchaseDetails=new OdPurchaseDetails();
        odPurchaseDetails.setPhCode(order.getPhCode());
        List<OdPurchaseDetails> list=odPurchaseOrderMapper.selectOdPurchaseDetailsList(odPurchaseDetails);
        order.setOdPurchaseDetailsList(list);
        PyAttachment pyAttachment=new  PyAttachment();
        pyAttachment.setOwnershipNumber(order.getPhCode());
        order.setFileList(Collections.singletonList(pyAttachmentMapper.selectPyAttachmentList(pyAttachment)));
        return order;
    }

    /**
     * 查询进货订单列表
     * 
     * @param odPurchaseOrder 进货订单
     * @return 进货订单
     */
    @Override
    public List<OdPurchaseOrder> selectOdPurchaseOrderList(OdPurchaseOrder odPurchaseOrder)
    {
        return odPurchaseOrderMapper.selectOdPurchaseOrderList(odPurchaseOrder);
    }

    @Override
    public List<OdPurchaseDetails> selectOdPurchaseDetailsList(OdPurchaseDetails odPurchaseDetails) {
        return odPurchaseOrderMapper.selectOdPurchaseDetailsList(odPurchaseDetails);
    }

    /**
     * 新增进货订单
     * 
     * @param odPurchaseOrder 进货订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOdPurchaseOrder(OdPurchaseOrder odPurchaseOrder)
    {
        odPurchaseOrder.setCreateTime(DateUtils.getNowDate());
        int rows = odPurchaseOrderMapper.insertOdPurchaseOrder(odPurchaseOrder);
        insertOdPurchaseDetails(odPurchaseOrder);
        return rows;
    }

    /**
     * 修改进货订单
     * 
     * @param odPurchaseOrder 进货订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOdPurchaseOrder(OdPurchaseOrder odPurchaseOrder)
    {
        System.out.println("xiugai"+odPurchaseOrder);
        List<OdPurchaseDetails> odPurchaseDetailsList = odPurchaseOrder.getOdPurchaseDetailsList();
        odPurchaseDetailsList.forEach(l->{
            odPurchaseOrderMapper.deleteDetailsByPhCode(l.getPhCode());
        });

        insertOdPurchaseDetails(odPurchaseOrder);
        return odPurchaseOrderMapper.updateOdPurchaseOrder(odPurchaseOrder);
    }

    /**
     * 批量删除进货订单
     * 
     * @param phIds 需要删除的进货订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdPurchaseOrderByPhIds(Integer[] phIds)
    {
        String[] values = new String[phIds.length];
        int index = 0;
        for (Integer phId : phIds) {
            OdPurchaseOrder order = odPurchaseOrderMapper.selectOdPurchaseOrderByPhId(phId);
            // 假设你想将每个订单的某个属性（例如 phCode）添加到 values 数组中
            values[index++] = order.getPhCode();
        }
        odPurchaseOrderMapper.deleteOdPurchaseDetailsByPhCodes(values);
        return odPurchaseOrderMapper.deleteOdPurchaseOrderByPhIds(phIds);
    }

    @Override
    public int deleteDetailsByPhCode(String phCode) {
        return odPurchaseOrderMapper.deleteDetailsByPhCode(phCode);
    }

    /**
     * 删除进货订单信息
     * 
     * @param phId 进货订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdPurchaseOrderByPhId(Integer phId)
    {

        OdPurchaseOrder  order=odPurchaseOrderMapper.selectOdPurchaseOrderByPhId(phId);
        odPurchaseOrderMapper.deleteOdPurchaseDetailsByPhCode(order.getPhCode());
        return odPurchaseOrderMapper.deleteOdPurchaseOrderByPhId(phId);
    }


    /**
     * 进货部门
     * @return
     */
    @Override
    public List<SysDept> selectAllSysDept() {
        return odPurchaseOrderMapper.selectAllSysDept();
    }
    /**
     * 根据父Id 查询部门
     */
    @Override
    public List<SysDept> selectAllSysDeptByParentId(Long deptid) {
        return odPurchaseOrderMapper.selectAllSysDeptByParentId(deptid);
    }

    /**
     * 根据部门id 查询用户
     * @param deptId
     * @return
     */
    @Override
    public List<SysUser> selectAllSysUser(Long deptId) {
        return odPurchaseOrderMapper.selectAllSysUser(deptId);
    }

    @Override
    public SysUser selectBySysUserName(String userName) {
        return odPurchaseOrderMapper.selectBySysUserName(userName);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param odPurchaseOrder 进货订单对象
     */
    public void insertOdPurchaseDetails(OdPurchaseOrder odPurchaseOrder)
    {
        List<OdPurchaseDetails> odPurchaseDetailsList = odPurchaseOrder.getOdPurchaseDetailsList();
        String phCode = odPurchaseOrder.getPhCode();
        if (StringUtils.isNotNull(odPurchaseDetailsList))
        {
            List<OdPurchaseDetails> list = new ArrayList<OdPurchaseDetails>();
            for (OdPurchaseDetails odPurchaseDetails : odPurchaseDetailsList)
            {
                odPurchaseDetails.setPhCode(phCode);
                odPurchaseDetails.setRejectedNum(0L);
                odPurchaseDetails.setIsDel("0");
                list.add(odPurchaseDetails);
            }
            if (list.size() > 0)
            {
                odPurchaseOrderMapper.batchOdPurchaseDetails(list);
            }
        }
    }

    @Override
    public int checkById(Map<String, Object> map) {
        return odPurchaseOrderMapper.checkById(map);
    }

    @Override
    public int revokeById(Integer phid) {
        return odPurchaseOrderMapper.revokeById(phid);
    }

    @Override
    public List<OdPurchaseOrder> selectOdPurchaseOrderByInRejected(OdPurchaseOrder odPurchaseOrder) {
        return odPurchaseOrderMapper.selectOdPurchaseOrderByInRejected(odPurchaseOrder);
    }

    @Override
    public int colseById(Integer phid) {
        return odPurchaseOrderMapper.colseById(phid);
    }


    @Override
    public Map<String,Object> selectByPhCode(String phCode) {
        return odPurchaseOrderMapper.selectByPhCode(phCode);
    }
}
